package za.co.green;

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class P2PFile {

	public static void main(String[] args) {

		args = new String[]{"235.255.255.255",
							"8080",
							"4561",
							"MyFileName.txt"};
		
		System.out.println("[P2P File Test] Starting ...");

		if (args == null
				|| (args.length != 3
				&& args.length != 4) ) {
			System.err.println("[P2P File Test] Usage; [Bind Address] " +
					"[UDP Multicast Port] " +
					"[TCP SC Port] " +
					"[optional file-name]");
			return;
		}

		for (String  i : args) {
			if (i == null || i.equals("")) {
				System.err.println("[P2P File Test] Usage; [Bind Address] " +
						"[UDP Multicast Port] " +
						"[TCP SC Port] " +
						"[optional file-name]");
				return;
			}
		}

		String bindAddress = "";

		int udpMultiPort = 0;

		int tcpScPort = 0;

		bindAddress = args[0];

		try {
		udpMultiPort = Integer.parseInt(args[1]);
		} catch (Exception e) { udpMultiPort = 0; }

		try {
		tcpScPort = Integer.parseInt(args[2]);
		} catch (Exception e) { tcpScPort = 0; }

		// check inputs
		if (bindAddress == null
				|| bindAddress.equals("")
				|| !bindAddress.contains(".")
				|| bindAddress.length() <= 5 ) {
			System.err.println("[P2P File Test] INVALID Bind Address");
			return;
		} else if (udpMultiPort == 0) {
			System.err.println("[P2P File Test] Invalid UDP Port");
			return;
		} else if (tcpScPort == 0) {
			System.err.println("[P2P File Test] Invalid TCP Port");
			return;
		}

		try {
			System.out.println("[P2P File Test] Creating UDPClientThread ...");
			new UDPClientThread(bindAddress,
					udpMultiPort, tcpScPort).start();
			
		} catch (Exception e)  {
			e.printStackTrace(); return;
			}
		
		// the server thread needs to communicate new files, so how are we going to tell a thread that there is work
		// mab a thread for each new file ? Then after it spams the world it just returns normally

		if (args[3] != null && !args[3].equals("")) {

			// if an file-name was given , try to get it at the app's current directory
			
			try {
				System.out.println("[P2P File Test] Creating UDPServerThread ...");
				new UDPServerThread(bindAddress,
						udpMultiPort, args[3]).start();
				
			} catch (Exception e) {
				e.printStackTrace(); return;
				}
		}

		try {
			System.out.println("[P2P File Test] Creating ServerSocketChannel ...");
			ServerSocketChannel serverSocketChannel
			= ServerSocketChannel.open();

			  serverSocketChannel.socket().bind(
					  new InetSocketAddress(tcpScPort));
			  serverSocketChannel.configureBlocking(false);

			  while (true) {
			      SocketChannel socketChannel =
			              serverSocketChannel.accept();

			      if (socketChannel != null) {
			          //do something with socketChannel...
			    	  // we could ma a new thread to handle each new request for a file..
			    	  System.out.println("[P2P File Test] Creating SocketChannel ...");
			      }
			  }

		} catch (Exception e) { e.printStackTrace(); return; }

	}

}
